package vip.liux.backend.application.serviceImpl.pay;


import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import vip.liux.application.dots.PagedResultDto;
import vip.liux.backend.application.service.pay.AutoMapperProfile;
import vip.liux.backend.application.service.pay.PayWalletService;
import vip.liux.backend.application.service.pay.PayWalletTransactionService;
import vip.liux.backend.application.service.pay.dto.wallet.PayWalletDto;
import vip.liux.backend.application.service.pay.dto.wallet.PayWalletTransactionDto;
import vip.liux.backend.application.service.pay.dto.wallet.PayWalletTransactionQueryDto;
import vip.liux.backend.application.service.pay.dto.wallet.WalletTransactionCreateDto;
import vip.liux.backend.application.serviceImpl.pay.client.WalletPayClient;
import vip.liux.backend.infrastructure.code.BizCodeGenerator;
import vip.liux.contracts.infrastructure.pay.impl.AbstractPayClient;
import vip.liux.contracts.models.pay.PayWalletTransaction;
import vip.liux.contracts.repositories.pay.PayWalletTransactionRepository;
import vip.liux.contracts.shared.enums.pay.PayChannelCode;
import vip.liux.contracts.shared.enums.pay.PayWalletBizType;

import java.util.Objects;
import java.util.function.Supplier;

import static vip.liux.application.PageKit.toPage;
import static vip.liux.application.PageKit.toPaged;

/**
 * 钱包流水 Service 实现类
 *
 * @author jason
 */
@Service
@Slf4j
@Validated
public class PayWalletTransactionServiceImpl implements PayWalletTransactionService {

    @Bean
    public Supplier<Pair<PayChannelCode, Class<? extends AbstractPayClient<?>>>> walletPayClient() {
        return () -> Pair.of(
                PayChannelCode.WALLET,
                WalletPayClient.class // 具体实现类
        );
    }

    @Resource
    private PayWalletService payWalletService;
    @Resource
    private PayWalletTransactionRepository payWalletTransactionRepo;
    @Resource
    private BizCodeGenerator codeGenerator;

    // @Override
    // public PageResult<PayWalletTransactionDO> getWalletTransactionPage(Long userId, Integer userType,
    //                                                                    AppPayWalletTransactionPageReqVO pageVO) {
    //     PayWalletDO wallet = payWalletService.getOrCreateWallet(userId, userType);
    //     return payWalletTransactionMapper.selectPage(wallet.getId(), pageVO.getType(), pageVO, pageVO.getCreateTime());
    // }

    @Override
    public PagedResultDto<PayWalletTransactionDto> getWalletTransactionPage(PayWalletTransactionQueryDto input) {
        // 基于 userId 查询钱包
        if (input.getWalletId() == null
                && Objects.nonNull(input.getUid())) {
            PayWalletDto wallet = payWalletService.getOrCreateWallet(input.getUid());
            if (wallet != null) {
                input.setWalletId(wallet.getId());
            }
        }

        // 查询分页
        return toPaged(
                payWalletTransactionRepo.findPage(input.getWalletId(), input.getUid(), toPage(input))
                        .map(AutoMapperProfile.INSTANCE::convert)
        );
    }

    @Override
    public PayWalletTransactionDto createWalletTransaction(WalletTransactionCreateDto input) {
        PayWalletTransaction transaction = AutoMapperProfile.INSTANCE.convert(input)
                .setSeqNo(codeGenerator.generateWalletTransaction());
        payWalletTransactionRepo.save(transaction);
        return AutoMapperProfile.INSTANCE.convert(transaction);
    }

    @Override
    public PayWalletTransactionDto getWalletTransactionBySeqNo(String seqNo) {
        return AutoMapperProfile.INSTANCE.convert(payWalletTransactionRepo.findBySeqNo(seqNo).orElseThrow());
    }

    @Override
    public PayWalletTransactionDto getWalletTransaction(String bizId, PayWalletBizType type) {
        return AutoMapperProfile.INSTANCE.convert(
                payWalletTransactionRepo.findByBizIdAndBizType(bizId, type).orElse(null)
        );
    }

    // @Override
    // public AppPayWalletTransactionSummaryRespVO getWalletTransactionSummary(Long userId, Integer userType, LocalDateTime[] createTime) {
    //     PayWalletDO wallet = payWalletService.getOrCreateWallet(userId, userType);
    //     return new AppPayWalletTransactionSummaryRespVO()
    //             .setTotalExpense(payWalletTransactionMapper.selectPriceSum(wallet.getId(), TYPE_EXPENSE, createTime))
    //             .setTotalIncome(payWalletTransactionMapper.selectPriceSum(wallet.getId(), TYPE_INCOME, createTime));
    // }

}
